#include <stdio.h>
#include <stdint.h>
#include "utils.h"

// 测试位串到8位字节串的转换
void test_bitstring_to_bytes() {
    const char *bitstring = "100110110";
    uint8_t bytes[4];
    size_t byte_len;
    bitstring_to_bytes(bitstring, bytes, &byte_len);
    printf("Testing bitstring_to_bytes:\n");
    printf("Bitstring: %s\n", bitstring);
    printf("Bytes: ");
    for (size_t i = 0; i < byte_len; ++i) {
        printf("%02X ", bytes[i]);
    }
    printf("\n");
}

// 测试8位字节串到位串的转换
void test_bytes_to_bitstring() {
    uint8_t bytes[] = {0x01, 0xB6};
    char bitstring[17]; // 8 * 2 + 1
    bytes_to_bitstring(bytes, sizeof(bytes), bitstring);
    printf("Testing bytes_to_bitstring:\n");
    printf("Bytes: ");
    for (size_t i = 0; i < sizeof(bytes); ++i) {
        printf("%02X ", bytes[i]);
    }
    printf("\nBitstring: %s\n", bitstring);
}

// 测试整数到8位字节串的转换
void test_int_to_bytes() {
    uint32_t value = 123456789;
    uint8_t bytes[4];
    int_to_bytes(value, bytes);
    printf("Testing int_to_bytes:\n");
    printf("Integer: %u\n", value);
    printf("Bytes: ");
    for (int i = 0; i < 4; ++i) {
        printf("%02X ", bytes[i]);
    }
    printf("\n");
}

// 测试8位字节串到整数的转换
void test_bytes_to_int() {
    uint8_t bytes[] = {0x04, 0xC5, 0x0A, 0x03};
    uint32_t result = bytes_to_int(bytes);
    printf("Testing bytes_to_int:\n");
    printf("Bytes: ");
    for (size_t i = 0; i < sizeof(bytes); ++i) {
        printf("%02X ", bytes[i]);
    }
    printf("\nInteger: %u\n", result);
}

int main() {
    printf("Starting tests...\n");

    // 测试所有四个功能
    test_bitstring_to_bytes();
    test_bytes_to_bitstring();
    test_int_to_bytes();
    test_bytes_to_int();

    printf("All tests completed.\n");
    return 0;
}
